home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Interplay's Learn to Program Basic (Review Copy)
/
Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO
/
pc
/
ltpbasic
/
projects
/
tank.bas
< prev
next >
Wrap
BASIC Source File
|
1998-03-02
|
7KB
|
318 lines
Rem Tank Battle
Rem Declare the arrays
Rem Use an array for the variables
Rem that control the tanks. That
Rem way, we can use a 1 for tank
Rem one and a 2 for tank two and
Rem not have to have a seperate
Rem variable for each tank.
Rem Plus, we could add more tanks,
Rem if we wanted to.
Dim X(2)
Dim Y(2)
Dim angle(2)
Dim bulletX(2)
Dim bulletY(2)
Dim bulletAngle(2)
Dim g$(2)
Dim l$(2)
Dim r$(2)
Dim f$(2)
Dim tank(2)
Dim bllspr(2)
Dim timelatch(2)
Dim hits(2)
Dim spin(2)
Rem This array is used to help match our direction number with the sprite frame number
Dim remap(8)
Rem this variable describes how many
Rem total obstacles, including
Rem both rocks and houses
Rem that we have in the data statements
lastObs = 7
Rem This array will hold the locations of all of our obstacles
Dim ObsX(lastObs)
Dim ObsY(lastObs)
Rem Set up the screen and sprites
cls
Background "Tank"
tank(1) = LoadSprite("bluetank")
tank(2) = LoadSprite("redtank")
house = LoadSprite("house")
Rem Sounds
Rem -- Slightly different each tank
Rem Sound of tank moving
Dim tankSnd(2)
tankSnd(1) = LoadSound("Tank1")
tankSnd(2) = LoadSound("Tank2")
Dim turnSnd(2)
turnSnd(1) = LoadSound("Squeak")
turnSnd(2) = LoadSound("Squeak")
Dim shootSnd(2)
shootSnd(1) = LoadSound("BigGun1")
shootSnd(2) = LoadSound("BigGun2")
Dim spinSnd(2)
spinSnd(1) = LoadSound("DuckDth")
spinSnd(2) = LoadSound("HeadShke")
Dim hitObsSnd(2)
hitObsSnd(1) = LoadSound("Punch")
hitObsSnd(2) = LoadSound("EyePop")
bombSnd = LoadSound("Exp_Med")
Rem This data is the 'remap' table for tank direction
data 0,6,2,7,1,4,3,5
Rem Create a table that will match the sprite frames to direction
for z = 1 to 8
read remap(z)
next z
Rem X-Y coordinates for obstacles
Rem All obstacles are assumed to be 32x32
Rem these are for the rocks
Rem that are part of the background
data 32,32,64,32,224,32,160,32
data 192,160,64,192
Rem remaining x,y pairs are for houses
data 125,140
Rem Read in data for obstacles
Rem The first 6 are for mountains
for i = 1 to lastObs
read ObsX(i)
read ObsY(i)
if i > 6 then
SetSprite house to ObsX(i),ObsY(i)
PasteSprite house
HideSprite house
endif
next i
Rem Set up bullet sprites
bllspr(1) = LoadSprite("bullet")
CycleSprite bllspr(1)
HideSprite bllspr(1)
bllspr(2) = LoadSprite("bullet")
CycleSprite bllspr(2)
HideSprite bllspr(2)
Rem Set up the data that defines the players
Rem position and key commands
lasttank = 2
g$(1) = "w"
l$(1) = "a"
r$(1) = "d"
f$(1) = "s"
angle(1) = 0
x(1) = 80
y(1) = 100
spin(1) = 0
timelatch(1) = timer()
bulletX(1) = -1
g$(2) = "i"
l$(2) = "j"
r$(2) = "l"
f$(2) = "k"
angle(2) = 3.1415
x(2) = 280
y(2) = 130
spin(2) = 0
bulletX(2) = -1
timelatch(2) = timelatch(1)
Rem Set up the turn delay and how long the game is
turndelay = 350
timeend = timer() + 120000
gameon = 1
while gameon
for b = 1 to lasttank
Rem Get keyboard commands from
Rem the current player
if keydown(g$(b)) then gosub Forward
if keydown(l$(b)) then gosub Left
if keydown(r$(b)) then gosub Right
if keydown(f$(b)) then gosub Fire
Rem If the tank is spinning, spin it!
if timer() < spin(b) then
fr = random(0,7)
angle(b) = .7853981 * fr
else
Rem set the proper frame for the tank sprite
fr = angle(b) / (2*3.1415926)
fr = fr * 8
fr = fr + .5
if fr >= 8 then fr = fr - 8
endif
SetSprite tank(b) frame remap(fr+1)
SetSprite tank(b) to x(b),y(b)
Rem If the bullet is active, update it
if bulletX(b) <> -1 then gosub Bullet
next b
Rem Update the information text
position 0,14
timeleft = int((timeend - timer())/1000)
if timeleft >0 then
print timeleft;" left Grey Pts:";hits(2);" Green Pts:";hits(1);" "
else
gameon = 0
endif
wend
Rem Time's up! Now print who scored
Rem the most hits.
cls
for t = 1 to lasttank
HideSprite bllspr(t)
HideSprite tank(t)
next t
print "Number of points per tank:"
print "Grey tank points:";hits(2)
print "Green tank points:";hits(1)
print
if hits(1) = hits(2) then
print "The match was a tie!"
else
if hits(1) > hits(2) then
print "Green tank wins!"
else
print "Grey tank wins!"
endif
endif
Sound "GamOver1"
banner "Game Over"
end
Forward:
Rem Use trigonometry to step the tank forward
incx = 2*cos(angle(b))
incy = 2*sin(angle(b))
PlaySound(tankSnd(b))
Rem first check if going forward would take the tank out of bounds
Rem or make it hit an obstacle
xhit = x(b) + incx + 16
yhit = y(b) + incy + 16
stop = 0
checkbullet = 0
gosub HitObstacle
Rem If all is OK, move the tank forward
if stop = FALSE then
x(b) = x(b) + incx
y(b) = y(b) + incy
endif
return
Rem For turning left and right, a timelatch is used to cause a third-second
Rem delay between turns
Rem Turn left
Left:
if timelatch(b) < timer() - turndelay then
PlaySound(turnSnd(b))
angle(b) = angle(b) - .7854
if angle(b) < 0 then angle(b) = 6.283
timelatch(b) = timer()
endif
return
Rem Turn right
Right:
if timelatch(b) < timer() - turndelay then
PlaySound(turnSnd(b))
angle(b) = angle(b) + .7854
if angle(b) > 6.283 then angle(b)= 0
timelatch(b) = timer()
endif
return
Rem Fire a tank bullet
Fire:
if bulletX(b) = -1 then
PlaySound(shootSnd(b))
bulletX(b) = x(b) + 15
bulletY(b) = y(b) + 11
bulletAngle(b) = angle(b)
endif
return
Rem Subroutine used to move the bullet - before each
Rem step, check if it hits an obstacle
Bullet:
bulletX(b) = bulletX(b) + (12*cos(bulletAngle(b)))
bulletY(b) = bulletY(b) + (12*sin(bulletAngle(b)))
xhit = bulletX(b) + 1
yhit = bulletY(b) + 1
checkbullet = 1
Rem check for obstacles that will stop the bullet
gosub HitObstacle
If Stop = FALSE Then
Rem Draw the bullet
SetSprite bllspr(b) to bulletX(b), bulletY(b)
Rem now check for tanks in its path
for z = 1 to lasttank
if z <> b then
tankx = x(z) + 15
tanky = y(z) + 15
Rem If we've hit this tank, spin it around...
If SpriteHitSprite(bllspr(b),tank(z)) = TRUE Then
PlaySound(spinSnd(z))
spin(z) = timer() + 1000
hits(z) = hits(z) + 1
stop = 1
endif
endif
next z
EndIf
if stop then
PlaySound(bombSnd)
HideSprite bllspr(b)
bulletX(b) = -1
bulletY(b) = -1
endif
return
Rem This subroutine will take the variables xhit and yhit
Rem and check them against the screen boundaries and whether
Rem they hit an obstacle. If any of these are true, stop will
Rem equal to one.
HitObstacle:
stop = 0
if xhit > 0 and xhit < 319 and yhit > 0 and yhit < 239 then
for i = 1 to LastObs
if checkbullet then
xob1 = ObsX(i)
xob2 = xob1 + 31 ' Obstacles are 32x32
yob1 = ObsY(i)
yob2 = yob1 + 31
else
xob1 = ObsX(i) - 16 ' Center point of a 32x32 obstacle
xob2 = xob1 + 31 + 16
yob1 = ObsY(i) - 16
yob2 = yob1 + 31 + 16
endif
Rem We can't use sprite collision test if sprite is pasted, so we check by coordinate
if xhit >= xob1 and xhit <= xob2 and yhit >= yob1 and yhit <= yob2 then
stop = 1
PlaySound(hitObsSnd(b))
Endif
next i
else
stop = 1
endif
return